'use strict';

function formatDate() {  
	const date = new Date();  
	const year = date.getFullYear();  
	const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的，所以要加1  
	const day = ('0' + date.getDate()).slice(-2);  
	return `${year}-${month}-${day}`;  
}
			
const db = uniCloud.database()
exports.main = async (event, context) => {
	// 剩余可用积分
	const res_integral = await db.collection('rx-clinic').where({ _id: event.clinicId }).field({ 'integral': true }).get()
	
	// 统计今日开单数
	const targetDate = formatDate() // 目标日期
	const res_rx = await db.collection('rx').where({ clinicId: event.clinicId, slipCreateTime: new RegExp(`^${targetDate}`) }).count()
	
	
	//返回数据给客户端
	return { data: {
		integral: res_integral.data[0].integral,
		today_statistics: res_rx.total,
	}, code: 200, msg: 'ok.' }
};
